package com.nabi.starter.common.base;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.nabi.starter.common.exception.ErrorCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;

import java.util.List;

@Getter
@Api("响应结果")
public class Result<T> {
    @ApiModelProperty("状态码")
    private Integer code;
    @Setter
    @ApiModelProperty("操作结果")
    private String msg;

    @Setter
    @ApiModelProperty("总条数")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long pageTotal;

    @Setter
    @ApiModelProperty("每页条数")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long pageSize;

    @Setter
    @ApiModelProperty("总页数")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long pages;

    @Setter
    @ApiModelProperty("当前页码")
    @JsonInclude(JsonInclude.Include.NON_NULL)
    private Long pageCurrent;

    @Setter
    @ApiModelProperty("结果集")
    private T result;


    public Result(ErrorCode code) {
        this.code = code.getCode();
        this.msg = code.getMsg();
    }

    public Result(ErrorCode code, T result) {
        this.code = code.getCode();
        if (result == null
                || (result instanceof List
                && ((List) result).size() <= 0)) {
            this.msg = "暂无数据";
        } else {
            this.result = result;
            this.msg = code.getMsg();
        }
    }
}
